from django.shortcuts import render
from rest_framework.generics import ListAPIView

from goods.models import SKU
from goods.serializers import SKUIndexSerializer
from users.serializers import SKUSerializer
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet


# Create your views here.

# 请求方式： GET /categories/(?P<category_id>\d+)/skus?page=xxx&page_size=xxx&ordering=xxx


class SKUListView(ListAPIView):
    """
    SKU商品列表
    """

    serializer_class = SKUSerializer
    filter_backends = (OrderingFilter,)

    ordering_fields = ('create_time', 'price', 'sales')

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]
    serializer_class = SKUIndexSerializer

